#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.9
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# Fitting -----------------------------------------------------------------
treat_baseline <- c("CH", "CS", "NM", "replication")
treat_interact <- c(treat_baseline, "CH:replication", "CS:replication", "NM:replication")

s9_1 <- noCov(immPCA, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_2 <- noCov(immPCA, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_2_test <- anova(s9_1, s9_2)

s9_3 <- allCov(immPCA, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_4 <- allCov(immPCA, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_4_test <- anova(s9_3, s9_4)

s9_5 <- noCov(imm_neighbors2, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_6 <- noCov(imm_neighbors2, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_6_test <- anova(s9_5, s9_6)

s9_7 <- allCov(imm_neighbors2, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_8 <- allCov(imm_neighbors2, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_8_test <- anova(s9_7, s9_8)

s9_9 <- noCov(imm_increase2, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_10 <- noCov(imm_increase2, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_10_test <- anova(s9_9, s9_10)

s9_11 <- allCov(imm_increase2, sample = "Pooled", treatments = treat_baseline, chatty = F)
s9_12 <- allCov(imm_increase2, sample = "Pooled", treatments = treat_interact, chatty = F)
s9_12_test <- anova(s9_11, s9_12)

# Reporting ---------------------------------------------------------------

# in the document, these three outputs are merged to a single latex table.

interact_coef_map <- list(
  "NM" =  "Norms",
  "CS" = "Countering Stereotypes",
  "CH" = "Common Humanity",
  "replication" = "Study: Replication",
  "NM:replication" = "Norms $\\times$ Study: Rep.",
  "CS:replication" = "C.S. $\\times$ Study: Rep.",
  "CH:replication" = "C.H. $\\times$ Study: Rep."
)

texregPS(
  list(s9_1, s9_2, s9_3, s9_4),
  custom.coef.map = interact_coef_map,
  custom.model.names = paste0("(", 1:4, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "", "$\\checkmark$", "$\\checkmark$"),
    "$F$-Statistic" = c("", round(s9_2_test$`F`[2], 2), "", round(s9_4_test$`F`[2], 2)),
    "$p$-value" = c("", round(s9_2_test$`Pr(>F)`[2], 2), "", round(s9_4_test$`Pr(>F)`[2], 2))
    )
)

texregPS(
  list(s9_5, s9_6, s9_7, s9_8),
  custom.coef.map = interact_coef_map,
  custom.model.names = paste0("(", 5:8, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "", "$\\checkmark$", "$\\checkmark$"),
    "$F$-Statistic" = c("", round(s9_6_test$`F`[2], 2), "", round(s9_8_test$`F`[2], 2)),
    "$p$-value" = c("", round(s9_6_test$`Pr(>F)`[2], 2), "", round(s9_8_test$`Pr(>F)`[2], 2))
  )
)

texregPS(
  list(s9_9, s9_10, s9_11, s9_12),
  custom.coef.map = interact_coef_map,
  custom.model.names = paste0("(", 9:12, ")"),
  custom.gof.rows = list(
    "All Covariates" = c("", "", "$\\checkmark$", "$\\checkmark$"),
    "$F$-Statistic" = c("", round(s9_10_test$`F`[2], 2), "", round(s9_12_test$`F`[2], 2)),
    "$p$-value" = c("", round(s9_10_test$`Pr(>F)`[2], 2), "", round(s9_12_test$`Pr(>F)`[2], 2))
  )
)

